package com.wc.AlgoOJ_homework.AlgoOJ_HomeWork2.W_巧克力;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/6 16:57
 * @description http://43.138.190.70:8888/p/505?tid=65e6b15c27489dab814f8d41
 */
public class Main {
    // 思路day 多的先进栈，记的需要用先吃最后一天的食物，保质期满足的食物，记得开long
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[][] a = new int[N][3];
    static int x, n;

    public static void main(String[] args) {
        x = sc.nextInt();
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < 3; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        // 按照保质期排序
        Arrays.sort(a, 1, n + 1, Comparator.comparingInt(o -> -o[1]));
        // 按照价格排序
        PriorityQueue<int[]> q = new PriorityQueue<>((Comparator.comparingInt(o -> o[0])));
        int idx = 1;
        long res = 0;
        // 双指针便利
        while (x > 0) {
            while (idx <= n && a[idx][1] >= x) {
                q.add(a[idx]);
                idx++;
            }

            if (q.isEmpty()) {
                res = -1;
                break;
            } else {
                int[] tmp = q.peek();
                res += tmp[0];
                // 数量减一
                tmp[2]--;
                // 天数要减一
                x--;
                if (tmp[2] == 0) q.poll();
            }
        }

        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
